import { defineStore } from 'pinia'
import { commonLoadEnumValue } from '@/api/common'
// 枚举 缓存
export const useEnumeration = defineStore('useEnumeration', {
  state: () => {
    return {
      markModeEnum: [], // 打分方式
      scoreCollectWayEnum: [], // 评分点汇总方式
      yesOrNoEnum: [], // 是否xx
      markScoreTypeEnum: [], // 打分类型
      flowOrderEnum: [], // 流程顺序
      calculateTypeEnum: [], // 计算公式
      stepTypeEnum: [] // 操作步骤类型
    }
  },
  persist: {
    storage: sessionStorage,
    paths: [
      'markModeEnum',
      'scoreCollectWayEnum',
      'yesOrNoEnum',
      'markScoreTypeEnum',
      'flowOrderEnum',
      'calculateTypeEnum',
      'stepTypeEnum'
    ]
  },
  actions: {
    // 获取枚举值 根据code获取key
    getEnumValue(selectCode: string, arrCode: string) {
      const item = this[arrCode].find((item: any) => item.key === selectCode)
      if (item) {
        return item.value
      }
      return ''
    },
    async getStepTypeEnum() {
      if (this.stepTypeEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'StepTypeEnum' })
        this.stepTypeEnum = res
      }
      return this.stepTypeEnum
    },
    async getMarkModeEnum() {
      if (this.markModeEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'MarkModeEnum' })
        this.markModeEnum = res
      }
      return this.markModeEnum
    },
    async getScoreCollectWayEnum() {
      if (this.scoreCollectWayEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'ScoreCollectWayEnum' })
        this.scoreCollectWayEnum = res
      }
      return this.scoreCollectWayEnum
    },
    async getYesOrNoEnum() {
      if (this.yesOrNoEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'YesOrNoEnum' })
        this.yesOrNoEnum = res
      }
      return this.yesOrNoEnum
    },
    async getMarkScoreTypeEnum() {
      if (this.markScoreTypeEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'MarkScoreTypeEnum' })
        this.markScoreTypeEnum = res
      }
      return this.markScoreTypeEnum
    },
    async getFlowOrderEnum() {
      if (this.flowOrderEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'FlowOrderEnum' })
        this.flowOrderEnum = res
      }
      return this.flowOrderEnum
    },
    async getCalculateTypeEnum() {
      if (this.calculateTypeEnum.length === 0) {
        const res: any = await commonLoadEnumValue({ typeCode: 'CalculateTypeEnum' })
        this.calculateTypeEnum = res
      }
      return this.calculateTypeEnum
    }
  }
})
